/**
 * Apr 3, 2010
 */
package org.tomale.id.gis.ui.internal;

import javax.media.opengl.GL;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.glu.GLU;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;

/**
 * @author ferd
 *
 */
public class MapCanvas extends GLCanvas {

	private final GLContext context;
	
	public MapCanvas(Composite parent, int style, GLData data) {
		super(parent, style, data);
		
		addEventListeners();
		
		this.setCurrent();
		context = GLDrawableFactory.getFactory().createExternalGLContext();
		
		init();
	}
	
	private void init(){
		this.setCurrent();
		context.makeCurrent();
//		resize();
		
		GL gl = context.getGL();
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//		gl.glClearColor(.3f, .5f, .8f, 1.0f);
		gl.glColor3f(0.0f, 0.0f, 0.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		
		context.release();
	}
	
	private void addEventListeners(){
		addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent e) {
				resize();
			}
			
			@Override
			public void controlMoved(ControlEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		addPaintListener(new PaintListener() {
			
			@Override
			public void paintControl(PaintEvent e) {
				paint();
			}
		});
		
		addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				switch(e.keyCode){
					case SWT.ARROW_UP:{
						// ctrl key was pressed?
						if((e.stateMask & SWT.CTRL) != 0){
							// rotate
						} else {
							// pan
						}
						break;
					}
					case SWT.ARROW_DOWN:{
						// ctrl key was pressed?
						if((e.stateMask & SWT.CTRL) != 0){
							// rotate
						} else {
							// pan
						}
						break;
					}
					case SWT.ARROW_LEFT:{
						// ctrl key was pressed?
						if((e.stateMask & SWT.CTRL) != 0){
							// rotate
						} else {
							// pan
						}
						break;
					}
					case SWT.ARROW_RIGHT:{
						// ctrl key was pressed?
						if((e.stateMask & SWT.CTRL) != 0){
							// rotate
						} else {
							// pan
						}
						break;
					}
					case SWT.PAGE_UP:{
						// zoom in
						break;
					}
					case SWT.PAGE_DOWN:{
						// zoom out
						break;
					}
					case SWT.HOME:{
						// center
						break;
					}
				}
			}
		});
		
		addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	
	private void resize(){
		Rectangle rect = this.getClientArea();
		int width = rect.width;
		int height = rect.height == 0 ? 1 : rect.height;
		float aspect = width/height;
		
		this.setCurrent();
		context.makeCurrent();
		GL gl = context.getGL();
		
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		GLU glu = new GLU();
		glu.gluPerspective(45.0f, aspect, 0.5f, 400.0f);
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		context.release();
	}
	
	private void paint(){
		this.setCurrent();
		GL gl = context.getGL();
		context.makeCurrent();
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
	    gl.glTranslatef(0.0f, 0.0f, -6.0f);
	    gl.glBegin(GL.GL_QUADS);
	    gl.glVertex3f(-1.0f, 1.0f, 0.0f);
	    gl.glVertex3f(1.0f, 1.0f, 0.0f);
	    gl.glVertex3f(1.0f, -1.0f, 0.0f);
	    gl.glVertex3f(-1.0f, -1.0f, 0.0f);
	    gl.glEnd();
	    
	    this.swapBuffers();
	    
	    context.release();
	}

}
